package src;// St�rikerfi 2, H�sk�linn � Reykjav�k.
//
// API fyrir Optimal s��uskiptareiknirit.  S��uskiptareikniriti� er 
// �tf�rt � klasanum src.OptimalMem.java.
//
// �lafur Ragnar Helgason (c), febr�ar 2002

import src.OptimalMem;

public class OptimalAlgorithm {

  public OptimalAlgorithm(int memSize, int memString[]) 
  {
    pageFaults = 0;
    memory = new OptimalMem(memSize);
    for (int i = 0; i < memString.length; i++) {
      memAccess(memString, i);
    }
  } 

  private void memAccess(int memString[], int strIdx) 
  {
    if (!memory.contains(memString[strIdx]))
      pageFault(memString, strIdx);
  }

  private void pageFault(int memString[], int strIdx) 
  {
    pageFaults++;
    memory.update(memString, strIdx);
  }

  public int pageFaults() 
  {
    return pageFaults;
  }

  private OptimalMem memory;
  private int pageFaults;
}
